<script lang="ts" setup>
// api
import type { FamilyData } from '@/types/types';
import { getPatientMylistAPI } from '@/api/healthEncyclopedia';
// com
import comScrollView from '@/components/common/com-scrollView/com-scrollView.vue';
import comNextButton from '@/components/common/com-nextButton/com-nextButton.vue';
import comMoveBox from '@/components/common/com-moveBox/com-moveBox.vue';
import comAddPatient from '@/components/common/com-addPatient/com-addPatient.vue';
import comIcon from '@/components/common/com-icon/com-icon.vue';
import comNavigation from '@/components/common/com-navigationBar/com-navigation.vue';
// vue
import { computed, onMounted, provide, ref, defineProps } from 'vue';
import { usePatientStore } from '@/store/modules/patient';
import { useRouter } from 'vue-router';
// familyManage 健康档案
// patient 患者
const props = defineProps<{ type: "familyManage" | 'patient' }>()
const store = usePatientStore()
const router = useRouter()
// 获取家庭档案列表
const list = ref<FamilyData[]>()
const active = ref<number>(0)
const flag = ref(false)
const getPatientMylistData = async () => {
  let res = await getPatientMylistAPI()
  list.value = res.data.data
  if (list.value.length === 0) {
    flag.value = true
  } else {
    flag.value = false
  }
}
const disabled = computed(() => {
  if (list.value) {
    if (list.value[active.value]) {
      return false
    } else {
      return true
    }
  } else {
    return true
  }
})
onMounted(() => {
  getPatientMylistData()
})
const show = ref(false)
// 获取选中的数据
const familyItem = ref<FamilyData>()
// 组件标题
const title = ref('添加患者')
const showAddPatient = (val?: number) => {
  show.value = true
  if (val) {
    title.value = '编辑患者'
  } else {
    title.value = '添加患者'
  }
  if (list.value) {
    familyItem.value = list.value[val as number]
  }
}
// 关闭添加
const close = () => {
  show.value = false
}
provide('close', close)
const checked = (index: number) => {
  active.value = index
  if (list.value) {
    store.patientInfo = list.value[index]
  }
}
const goToConsultPay = () => {
  router.push('/consultPay')
}
const overFn = () => {
  getPatientMylistData()
  show.value = false
}

</script>
<template>
  <comScrollView>
    <comNavigation>
      <template v-slot:title>
        <div class="title">家庭档案</div>
      </template>
    </comNavigation>
    <div class="box1">
      <p class="select" v-if="props.type === 'patient'">请选择患者信息</p>
      <p class="tips">{{ props.type === 'familyManage' ? '最多可添加6人' : '以便医生给出更准确的治疗，信息仅医生可见' }}</p>
      <div class="family-cart">
        <div class="family-item" @click="checked(index)" :class="active === index ? 'family-defalut' : ''"
          v-for="(item, index) in list" :key="item.id">
          <div class="left">
            <div class="top">
              <div class="defalutBox" v-if="item.defaultFlag === 1">
                <span class="defalut">默认</span>
              </div>
              <span class="top_name" v-else>{{ item.name }}</span>
              <span class="cartId">{{ item.idCard }}</span>
            </div>
            <div class="name" v-if="item.defaultFlag === 1">
              {{ item.name }}
            </div>
            <div class="bottom">
              <span class="sex">{{ item.genderValue }}</span>
              <span class="age">{{ item.age }}岁</span>
            </div>
          </div>
          <comIcon @click="showAddPatient(index)" class="editIcon editGreen" v-show="active === index"
            name="icons-user-edit">
          </comIcon>
          <comIcon @click="showAddPatient(index)" class="editIcon" name="icons-user-edit" v-show="active !== index">
          </comIcon>
        </div>
      </div>
      <div class="add" @click="showAddPatient()">
        <comIcon class="addIcon" name="icons-user-add"></comIcon>
        <span>添加患者</span>
      </div>
      <div class="btn">
        <comNextButton @onClick="goToConsultPay" :disabled="disabled"></comNextButton>
      </div>
    </div>
  </comScrollView>
  <comMoveBox :show="show">
    <comAddPatient type="h" :flag="flag" :data="familyItem" @fn="overFn" :title="title"></comAddPatient>
  </comMoveBox>
</template>
<style lang="scss" scoped>
.title {
  font-size: 17px
}

.box1 {
  padding: 0 15px;
  padding-bottom: 50px;

  p {
    margin: 0;
  }

  .select {
    font-size: 20px;
    height: 32px;
    line-height: 32px;
    margin-bottom: 5px;
  }

  .tips {
    font-size: 13px;
    color: #6f6f6f;
    margin-bottom: 25px;
  }

  .family-cart {
    .family-item {
      border: 1px solid transparent;
      padding: 22px 15px;
      display: flex;
      justify-content: space-between;
      background-color: #fafafa;
      margin-bottom: 10px;
      align-items: center;

      .left {
        .top {
          display: flex;
          justify-content: space-between;
          align-items: end;
          margin-bottom: 5px;
          line-height: 22px;

          .defalutBox {
            height: 100%;

            .defalut {
              background-color: #16c2a3;
              color: #fff;
              font-size: 9px;
              height: 14px;
              padding: 3px;
              margin-right: 10px;
            }
          }

          .top_name {
            font-size: 16px;
            font-weight: 500;
            margin-right: 20px;
          }

          .cartId {
            font-size: 14px;
            color: #6f6f6f;
          }
        }

        .name {
          font-size: 16px;
          font-weight: 500;
          margin-right: 20px;
          margin-bottom: 5px;
        }

        .bottom {
          span {
            font-size: 14px;
            margin-right: 20px;
            color: #6f6f6f;
          }
        }
      }

      .editGreen {
        color: #21c5a8;
      }

      .editIcon {
        width: 14px;
        height: 14px;
      }
    }

    .family-defalut {
      border: 1px solid #16c2a3;
      background-color: #f0faf9;
      border-radius: 5px
    }
  }

  .add {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px 0;
    background-color: #fafafa;

    .addIcon {
      width: 18px;
      height: 18px;
      color: #16c2a3;
    }

    span {
      font-size: 12px;
      color: #16c2a3;
      margin-top: 10px;
    }
  }

  .btn {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    box-sizing: border-box;
    padding: 5px 15px;
    font-size: 16px;
  }
}
</style>
